package net.jxta.myjxta.plugins.vojxta;

import net.jxta.endpoint.Message;
import net.jxta.myjxta.MyJXTA;
import net.jxta.myjxta.dialog.*;
import net.jxta.myjxta.plugin.IPluginNotificationHandler;
import net.jxta.myjxta.plugin.ISelectableNode;
import net.jxta.myjxta.plugin.PluginBase;
import net.jxta.myjxta.plugin.PluginContainer;
import net.jxta.myjxta.plugins.vojxta.command.VOJXTACommand;
import net.jxta.myjxta.util.Group;
import net.jxta.myjxta.util.Resources;
import net.jxta.myjxta.util.objectmodel.JxtaNode;
import net.jxta.myjxta.util.objectmodel.PeerNode;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.util.JxtaBiDiPipe;
import org.xiph.speex.spi.SpeexEncoding;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ResourceBundle;

public class VoicePlugin extends PluginBase implements IPluginNotificationHandler, PluginContainer.IPopupProvider,DialogManager.IDialogProvider {

    private static final ResourceBundle STRINGS = Resources.getStrings();

    public VoicePlugin() {
        setName(STRINGS.getString("menu.peer.voice"));
        try {
        	if (SpeexEncoding.class.getName().length()==0){
        		throw new Exception("can not happen");
        	}
        } catch (Exception e){
        	throw new IllegalStateException("can not find Voice Codec, maybe the jars are missig?");
        }
    }

    public IPluginNotificationHandler getPluginNotificationHander() {
        return this;
    }

    public void destroy() {
        super.destroy();    //To change body of overridden methods use File | Settings | File Templates.
        CommandFactory.registerCommand(VOJXTACommand.class);
    }

    public void init(PluginContainer c) {
        super.init(c);

        CommandFactory.registerCommand(VOJXTACommand.class);

        Dialog.registerDialogNamer(VoJxtaDialog.class, new DialogNamer() {
            public String getDialogName(String s) {
                        return VoJxtaDialog.DIALOG_NAME +
                            Dialog.IMFREE_DELIMITER + s;
                    };
        });

    DialogManager.registerDialogProvider(VoJxtaDialog.class,this);

    m_container.registerPopupProvider(this);

    start(); //autostart - we want the menu entry immediately after the initialisation

    }



    public void groupJoined(final Group p_group) {
        //on any group join we register our dialog name (--> dialog bidi-pipe) at the
        //correct dialogmanager
    	if (!p_group.isVisible()) //no listeners for npg and other non-visible groups
    		return; 
    	
        String dialogName1;
        PeerGroup pg = p_group.getPeerGroup();
        dialogName1 = Dialog.getDialogNamer(VoJxtaDialog.class).
                getDialogName(pg.getPeerName());

        final MyJXTA myJxta = m_container.getMyJxta();
        myJxta.setStatus(STRINGS.getString("status.dialog.listener.add") +
                ": " + dialogName1);


        // if we  get an incomming bidi-pipe request....
        DialogManager.getInstance(p_group, dialogName1, PipeService.UnicastType).
                addPipeListener(pg, new DialogPipeListener() {
                    // xxx: authenticate if not. for now, we are authenticated

                    public void receive(PeerGroup pg11, JxtaBiDiPipe pipe) {
                        myJxta.addDialog(DialogManager.getDialog(VoJxtaDialog.class,
                                p_group, pipe, myJxta));
                    }

                    public void receive(PeerGroup pg11, Message msg) {
                    }
                });
    }

    public void groupResigned(Group p_group) {
    	
    	if (!p_group.isVisible())
    		return;

        String pn = Dialog.getDialogNamer(VoJxtaDialog.class). getDialogName(p_group.getPeerGroup().getPeerName());
        m_container.getMyJxta().setStatus(STRINGS.getString("status.dialog.listener.remove") +
                ": " + pn);

        DialogManager.getInstance(p_group, pn, PipeService.UnicastType).
                clearPipeListeners(p_group.getPeerGroup());
    }

    public void groupStateChanged(Group p_group) {
        //nothing in here - this method is called if we get connected/disconnected from a joined group
        //we could possibly activate/deactivate the menu here....
    }


    public void popupRequested(PluginContainer.IPopupGenerator popupGenerator, ISelectableNode[] selectedNodes, MouseEvent triggerEvent) {

        if (!isRunning())
            return;
        if (selectedNodes!=null && selectedNodes.length>=1){
            JxtaNode jxtaNode = selectedNodes[0].getJxtaNode();
            if (jxtaNode instanceof PeerNode) {
                PeerNode peerNode = (PeerNode) jxtaNode;
                PluginContainer.MenuPath peerPath = new PluginContainer.MenuPath(Resources.getStrings().getString("menu.peer"), KeyEvent.VK_P);

//                Group g=((GroupNode)  MyJxtaObjectRepository.getObjectRepository().getParent(peerNode)).getGroup();
//                popupGenerator.addPopup(new PluginContainer.MenuPath[]{peerPath},3,(AbstractAction) new VoJxtaInviteAction(m_name,m_container.getMyJxta().getView()));
            }
        }
    }

    public Dialog createDialog(Group p_g, PipeAdvertisement p_pa, MyJXTA p_myjxta) {
        return new VoJxtaDialog(p_g, p_pa, p_myjxta);
    }

    public Dialog createDialog(Group p_g, JxtaBiDiPipe p_pipe, MyJXTA p_myjxta) {
        return new VoJxtaDialog(p_g, p_pipe, p_myjxta);
    }
}
